package com.neusoft.neuiotms.dcvm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.neuiotms.dcvm.bean.AttrBean;
import com.neusoft.neuiotms.dcvm.bean.StandardCiBean;
import com.neusoft.neuiotms.dcvm.service.AssetService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("资产管理")
@RestController
@RequestMapping(value = {"/api/w/assets", "/api/w/asset" })
public class AssetController {

    @Autowired
    private AssetService assetService;

    @ApiOperation("根据父容器编码获取该容器下所有设备信息")
    @GetMapping("/location/{locCode}/{deep}")
    public ResultListDTO<?> assetsOfLocation(@PathVariable final String locCode, @PathVariable final int deep) {
        final List<StandardCiBean> res = this.assetService.getLocDeepCi(locCode, deep);
        return ResultListDTO.success(res);
    }

    @ApiOperation("根据父容器编码获取该容器下所有设备信息")
    @GetMapping("/location/{locCode}/{deep}/simple")
    public ResultListDTO<?> assetsOfLocationSimple(@PathVariable final String locCode, @PathVariable final int deep) {
        final List<StandardCiBean> res = this.assetService.getLocDeepCi(locCode, deep);
        res.stream().forEach(it -> {
            it.getSchemadef().setAttrs(null);
            it.getRelations().clear();
        });
        return ResultListDTO.success(res);
    }

    @ApiOperation("资源详情")
    @GetMapping("/{code}")
    public ResultDTO<?> assetDetail(@PathVariable final String code) {
        final StandardCiBean res = this.assetService.getCiDetail(code);
        return ResultDTO.success(res);
    }

    @ApiOperation("修改3D属性信息")
    @PutMapping("/{code}/attrs")
    public ResultDTO<?> updateAssetAttrs(@PathVariable final String code, @RequestBody final List<AttrBean> attrs) {
        final StandardCiBean res = this.assetService.updateCiAttrs(code, attrs);
        return ResultDTO.success(res);
    }

    @ApiOperation("获取所有配电柜抽屉")
    @GetMapping("/pdgct/all")
    @Cacheable(value = "allPdgcts")
    public ResultListDTO<?> allPdgcts() {
        final List<StandardCiBean> res = this.assetService.getAllCiByCatetories("CT_PDCT");
        return ResultListDTO.success(res);
    }

    @ApiOperation("获取分类下所有资产")
    @GetMapping("/category/{code}")
    public ResultListDTO<?> allAssets(@PathVariable final String code) {
        final List<StandardCiBean> res = this.assetService.getAllCiByCatetories(code);
        return ResultListDTO.success(res);
    }

    @ApiOperation("获取分类下所有资产")
    @GetMapping("/category/{code}/simple")
    public ResultListDTO<?> allAssetsKvpairs(@PathVariable final String code) {
        final List<StandardCiBean> res = this.assetService.getAllCiByCatetories(code);
        final List<Map<String, String>> list = res.stream().map(it -> {
            final Map<String, String> map = new HashMap<>();
            map.put("code", it.getCode());
            map.put("name", it.getName());

            return map;
        }).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

    @ApiOperation("获取模型下所有资产")
    @GetMapping("/schema/{code}")
    public ResultListDTO<?> allCiBySchema(@PathVariable final String code) {
        final List<StandardCiBean> res = this.assetService.getAllCiBySchema(code);
        return ResultListDTO.success(res);
    }

    @ApiOperation("获取模型下所有资产")
    @GetMapping("/schema/{code}/simple")
    public ResultListDTO<?> allCiBySchemaKvpairs(@PathVariable final String code) {
        final List<StandardCiBean> res = this.assetService.getAllCiBySchema(code);
        final List<Map<String, String>> list = res.stream().map(it -> {
            final Map<String, String> map = new HashMap<>();
            map.put("code", it.getCode());
            map.put("name", it.getName());

            return map;
        }).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

    @ApiOperation("获取外键下所有资产")
    @GetMapping("/foreign/{foreignKey}/{foreignValue}")
    public ResultListDTO<?> allCiByForeignInst(@PathVariable final String foreignKey,
            @PathVariable final String foreignValue) {
        final List<StandardCiBean> res = this.assetService.getAllCiByForeignInst(foreignKey, foreignValue);
        return ResultListDTO.success(res);
    }

    @ApiOperation("机柜租赁情况分布")
    @GetMapping("/rackLeaseStatus/{PosCode}")
    public ResultDTO<?> rackLeaseStatus(@PathVariable final String PosCode) {
        final List<JSONObject> result = this.assetService.rackLeaseStatus(PosCode);
        if (result == null) {
            return ResultDTO.failure();
        }
        return ResultDTO.success(result);
    }

    @ApiOperation("机柜租赁类型分布")
    @GetMapping("/rackType/{PosCode}")
    public ResultDTO<?> rackType(@PathVariable final String PosCode) {
        final List<JSONObject> result = this.assetService.rackType(PosCode);
        if (result == null) {
            return ResultDTO.failure();
        }
        return ResultDTO.success(result);
    }

    @ApiOperation("查询设备的位置")
    @GetMapping("/findLocation/{assetCode}")
    public ResultDTO<?> findAssetLocation(@PathVariable final String assetCode) {
        final Map<String, Object> result = this.assetService.getAssetLocation(assetCode);
        return ResultDTO.success(result);
    }

    @ApiOperation("机柜容量")
    @GetMapping("/rackCapacity/{PosCode}")
    public ResultDTO<?> rackCapacity(@PathVariable final String PosCode) {
        final List<Map<String, Object>> result = this.assetService.rackCapacity(PosCode);
        if (result == null) {
            return ResultDTO.failure();
        }
        return ResultDTO.success(result);
    }
}
